﻿using System;
using System.Linq;
using System.Windows.Controls;
using ESRI.ArcGIS.Client;
using ESRI.ArcGIS.Client.Geometry;
using QDP.Platform.ObjService;
using QDP.Platform.Silverlight.Tools;
using Services.Maps;
namespace Apps.Maps
{
    public partial class MapContainer : UserControl, IMapService
    {
        public MapContainer()
        {
            InitializeComponent();
        }

        private void LoadService(Map map)
        {
            InitializeMap(map);
            int i = 0;
            // 处理自动加载的数据服务。
            var mapServices =  (from service in Q.Config.GisServiceConfig.MapServices select service).ToList();
            if (mapServices.Count > 0)
            {
                mapServices = NavigationDirectoryHelper.RemoveDirectory(mapServices.ToList());

                mapServices.ForEach(p =>
                {
                    if (p is ServiceLayer)
                    {
                        var layobj = p as ServiceLayer;

                        if (layobj.ParmsExpression.IndexOf("autoload;") < 0)
                            return;

                        if (InitializeExtent == null)
                            InitializeExtent = layobj.GetDefaultEnvelope();
                    }

                    var layer = LayerFactory.CreateLayer(p);
                    layer.InitializationFailed += (s, e) => { };
                    layer.Initialized += (s, e) =>
                    {
                        if (InitializeExtent == null) return;
                        map.ZoomTo(InitializeExtent);
                    };
                    map.Layers.Insert(i++, layer);
                    layer.Initialize();
                });
            }
        }

        #region IMapControl
        public Envelope InitializeExtent { get; set; }
        public Draw OtherDraw { get; set; }

        private void InitializeMap(Map map)
        {
            map.IsLogoVisible = false;
            map.ExtentChanging += (s, e) =>
            {
                if (this.ExtentChanging != null)
                {
                    this.ExtentChanging(this, e);
                }
            };
            DrawTool = new Draw(CurrentMap);
            DrawTool.DrawBegin += (send, args) =>
            {
                if (DrawBegin != null)
                {
                    DrawBegin(send, args);
                }
            };
            DrawTool.DrawComplete += (send, args) =>
            {
                if (DrawComplete != null)
                {
                    DrawComplete(send, args);
                }
            };
        }
        /// <summary>
        /// 显示默认地图
        /// </summary>
        public void ShowNorMap()
        {
            LoadService(CurrentMap = new Map() { AllowDrop=true});
            panelSingle.Child = CurrentMap;
            DrawTool.Map = CurrentMap;
            CurrentMap.ExtentChanged += new EventHandler<ExtentEventArgs>(CurrentMap_ExtentChanged);
        }

        void CurrentMap_ExtentChanged(object sender, ExtentEventArgs e)
        {
            InitializeExtent = e.NewExtent;
            CurrentMap.ExtentChanged -= CurrentMap_ExtentChanged;
        }

        public void ImportService(ServiceLayer service)
        {
            if (service == null) return;
            CurrentMap.UpdateLayout();
            Layer layer = LayerFactory.CreateLayer(service);
            layer.InitializationFailed += (s, e) => { };
            CurrentMap.Layers.Add(layer);
            layer.Initialize();
        }
        
        public Map CurrentMap { get; private set; }
        public Draw DrawTool { get; private set; }

        public event EventHandler DrawBegin;
        public event EventHandler<DrawEventArgs> DrawComplete;
        public event EventHandler<ExtentEventArgs> ExtentChanging;
        #endregion //#region IMapControl
    }
}